<template>
  <div class="app-container">
    <el-card v-loading="loading">
      <div slot="header">
        <el-button type="primary" @click="resetForm()">返回</el-button>
      </div>
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="仓库名称" prop="repositoryName">
          <el-input placeholder="仓库名称" v-model="formData.repositoryName"/>
        </el-form-item>
        <el-form-item label="仓库管理员" prop="">
			  <el-select v-model="formData.manageUserId" filterable placeholder="仓库管理员">
			    <el-option
			      v-for="item in managerUserIds"
			      :key="item.userId"
			      :label="item.userName"
			      :value="item.userId">
			    </el-option>
			  </el-select>
        </el-form-item>
        <el-form-item label="仓库地址" prop="repositoryAddress">
          <el-input placeholder="仓库地址" v-model="formData.repositoryAddress"/>
        </el-form-item>
        <el-form-item>
          <el-button @click="resetForm">关闭</el-button>
          <el-button :loading="isAdd" type="primary" @click="submitForm('ruleForm')">
            <span v-if="isAdd">提交中...</span>
            <span v-else>提交</span>
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { queryRepositoryDetail, newRepository, updateRepository } from '@/api/essentialInformation.js'
import { findAuditUsers } from "@/api/user"
let $this = null
export default {
  name: 'submitForm',
  props: {
    subData: {
      type: Object,
      default: {}
    }
  },
  data () {
    return {
      isAdd: false,
      loading: false,
      formData: {},
      rules: {},
      managerUserIds:[]
    }
  },
  created () {
    $this = this
    if ($this.subData.btnId == 1) {
      $this.queryRepositoryDetail()
    }
  $this.initManageUserIds();
  },
  methods: {
    initManageUserIds(){
      findAuditUsers().then(res=>{
        $this.managerUserIds=res.data.data && res.data.data
      })
    },
    // 详情
    queryRepositoryDetail () {
      $this.loading = true
      queryRepositoryDetail($this.subData.id).then(res => {
        $this.loading = false
        if (res.data.code != 200) {
          return $this.$message.error(res.data.message)
        }
        $this.formData = res.data.data
      }).catch(err => { $this.loading = false })
    },
    submitForm (formName) {
      $this.$refs[formName].validate((valid) => {
        if (!valid) {
          return $this.$message.error('请完善表单信息')
        }
        if ($this.subData.btnId == 0) {
          $this.isAdd = true
          newRepository($this.formData).then(res => {
            $this.isAdd = false
            if (res.data.code != 200) {
              return $this.$message.error(res.data.message)
            }
            $this.$emit('refresh')
          }).catch(err => { $this.isAdd = false })
        }
        if ($this.subData.btnId == 1) {
          $this.isAdd = true
          updateRepository($this.formData).then(res => {
            $this.isAdd = false
            if (res.data.code != 200) {
              return $this.$message.error(res.data.message)
            }
            $this.$emit('refresh')
          }).catch(err => { $this.isAdd = false })
        }
      })
    },
    resetForm () {
      $this.$emit('closeView')
    }
  }
}
</script>

<style scoped lang="scss">
  .el-input {
    width: 500px;
  }
</style>
